package 算法.线性表;

/**
 * @author: 谢君臣
 * @create: 2020-12-08 11:08
 * @founction: XXXX
 */
public class 手动实现ArrayList {
    public static void main(String[] args) {
        ArraysList list = new ArraysList(5);
        list.add(1);
        list.add(1);
        list.add(1);
        list.add(1);
        list.add(1);
        list.add(1);
        System.out.println(list.getSize());
    }
}

class ArraysList {
    private Object[] elementData;
    private int size = 0;

    public ArraysList() {
        this.elementData = new Object[10];
    }

    public ArraysList(int size) {
        if (size < 0)
            throw new RuntimeException("参数不合法,size:" + size);
        this.elementData = new Object[size];
    }

    public int getSize() {
        return size;
    }

    public void add(Object o) {
        if (this.size == this.elementData.length) {
            // 数组需要扩容
            System.out.println("数组需要扩容");
            Resize();
        }
        elementData[size++] = o;

    }

    private void Resize() {
        Object[] newData = new Object[size << 1];
        transfer(newData,elementData,0);
        elementData = newData;
    }

    public Object remove(int index) {
        Object old = new Object();
        if (index >= size)
            throw new RuntimeException("输入的下标越界了");
        old = elementData[index];
        //移动元素


        return old;
    }
    private void transfer(Object[] o1,Object[] o2,int index){
        for (int i = index; i < o2.length; i++) {
            o1[i] = o2[i];
        }
    }

}